'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 同意好友申请
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var PlayerManager = require('../../../../libs/playerManager');
var Code = require('../../../../consts/code');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var friend_role_id = msg.friend_role_id;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'friend', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var my_friend_model = player_manager.friend;

                // 拒绝好友申请
                my_friend_model.removeRequest(friend_role_id);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
